package com.orhon.smartcampus.modules.user.mutation;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlInputModel.GQLUserInput;
import com.orhon.smartcampus.modules.user.inputMapper.UserInputMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddUserMutation implements DataFetcher {

    @Autowired
    UsersDao usersDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Override
    @Transactional(readOnly = false)
    public Object get(DataFetchingEnvironment env) throws Exception {

        GQLUserInput input = UserInputMapper.MAPPER.mapInput(env.getArgument("inputData"));

        Users users = null;
        
        if (input.getId() != null){
            users = usersDao.get(Users.class, input.getId());
            users.setUpdated_at(new Date());
        }else {
        	String name = input.getUsername();
    		String hql = "from Users where deleted_at = null and username='"+name+"'";
    		List<Users> find = usersDao.findHql(hql);
    		if(find.size()>0) {
        		return Result.error(401, "用户名已存在!");
        	}
        	
            users = new Users();
            users.setCreated_at(new Date());
        }
        if(input.getEmail() != null){

            users.setEmail(input.getEmail());
        }
        if(input.getUsername() != null){

            users.setUsername(input.getUsername());
        }
        if(input.getPassword() != null && !input.getPassword().equals("")){
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(input.getPassword());
            users.setPassword(encode);
        }
        if(input.getIdcard() != null){

            users.setIdCard(input.getIdcard());
        }
        if (input.getDeleted_at() != null){
            users.setDeleted_at(input.getDeleted_at());
        }
        if(input.getUser_type() != null){

            users.setUser_type(input.getUser_type());
        }
        //"学校"
        if(input.getSchool() != null) {
            users.setSchool(schoolsDao.get(Schools.class, input.getSchool()));
        }
        if (input.getStatus() != null){
            users.setStatus(input.getStatus());
        }
        if(input.getSystem_lang() != null){

            users.setSystem_lang(input.getSystem_lang());
        }
        if(input.getUnion_id() != null){

            users.setUnionid(input.getUnion_id());
        }
        if(input.getMobile() != null){

            users.setMobile(input.getMobile());
        }
        if(input.getApi_token() != null){

            users.setApi_token(input.getApi_token());
        }
        if(input.getEquipment() != null){

            users.setEquipment_id(input.getEquipment());
        }
        if(input.getLast_login_ip() != null){

            users.setLast_login_ip(input.getLast_login_ip());
        }
        if(input.getLast_login_time() != null){

            users.setLast_login_time(input.getLast_login_time());
        }
        if(input.getRemember_token() != null){

            users.setRemember_token(input.getRemember_token());
        }
        try {
            usersDao.saveOrUpdate(users);
        } catch (Exception e) {
            return Result.error(500,""+e);
        }
        return Result.ok().put("data",users);
    }

    private String convertJavaBcryptPassword(String oldPass) {
        String old_password = oldPass;
        String new_password = "";
        if (old_password.charAt(2) == 'a') {
            StringBuilder strBuilder = new StringBuilder(old_password);
            strBuilder.setCharAt(2, 'y');
            new_password = strBuilder.toString();
            return new_password;
        } else {
            return old_password;
        }
    }
}
